#!/usr/bin/perl
#
# Patch f_ctime for objects that have recdate
#
use FileTable;
use HTTP::Date;
use Fcntl;

$DEBUG = 0;

#die "DO NOT USE THIS; use dbtxt2ctime\n";

$filename = shift;
$reckey = shift;

if (! -f "$filename.conf") {
    $filename = $ENV{"COUNTER_ROOT"} . "/db/" . $filename;
}

$file = FileTable::open($filename, O_RDWR);
$rec = $file->get($reckey);

    if ($rec->{recdate}) {
	$recdate = $rec->{recdate};
	$timestamp = str2time($recdate);
	# Patch for timezone not handled by HTTP::Date
	if (!defined($timestamp) && $recdate =~ /MET DST/) {
	    $recdate =~ s/MET DST/MEST/;
	    $timestamp = str2time($recdate);
	}
	if (defined($timestamp)) {
	    $rec->{f_ctime} = HTTP::Date::time2iso($timestamp);
	    print $rec->key(), ":", "Set ctime to $$rec{f_ctime}\n" if $DEBUG;
	    # Extremely dirty trick: Bypass the timestamping
	    bless $rec, FileTable::Record;
	    $rec->store("NOTEXT");
	    ++ $act{changed};
	} else {
	    print $rec->key(), ":";
	    print "Unparseable recdate: $$rec{recdate}\n";
	    ++ $act{unparsed};
	}
    } else {
	print $rec->key(), ":";
	print "Recdate is missing\n";
	++ $act{missing};
    }

for $act (keys(%act)) {
    print $act{$act}, " ", $act, "\n";
}

